package com.houdoudou.affair.jsp;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.houdoudou.bean.Distribute;
import com.houdoudou.bean.Machine;
import com.houdoudou.bean.Owner;
import com.houdoudou.bean.Supplier;
import com.houdoudou.dao.ConnFactory;
import com.houdoudou.dao.DistributeDao;
import com.houdoudou.dao.MachineDao;
import com.houdoudou.dao.OwnerDao;
import com.houdoudou.dao.SupplierDao;

public class OwnerAffairJsp1 {

/*------------------------------------------业主登陆-------------------------------------------------*/	
	
	public static void login(HttpServletRequest req, HttpServletResponse resp){		
		
		
		
	    String	jumpJspPath= "/owner/ownerLogin.jsp";
	    Owner owner;
		 Owner ownerExist= new Owner();
		//获取Bean---------------------------	
		 try{
		 owner= Owner.beanFromRequest(req);
		owner= Owner.encrypt(owner);
		System.out.println(""+owner.getSecret()+owner.getTelnum());
		 }catch(Exception e){
			 e.printStackTrace();
			  Common.forwardToJsp(req,resp,jumpJspPath,"输入参数错误");
			  return;
		 }
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{
			
		   conn= ConnFactory.getConn();
	
		   OwnerDao ownerDao= new  OwnerDao(conn);
		   ownerExist= ownerDao.selectByTelnum(owner.getTelnum());
		   if(ownerExist.getTelnum().isEmpty()){
			   Common.forwardToJsp(req,resp,jumpJspPath,"用户不存在"); return;
		   }else{
			   if(ownerExist.getSecret().equals(owner.getSecret())){
				   req.getSession().setAttribute("ownerTelnum", ownerExist.getTelnum());
				   req.getSession().setAttribute("ownerLogined", ownerExist);
				//	 req.getSession().setAttribute("programmerSecret", ownerExist.getSecret());
				  //初始化一些共用数据
				   MachineDao machineDao= new MachineDao(conn);
					  Machine machine= new Machine();  machine.setOwner(ownerExist.getTelnum());
					  List<Machine> machines= machineDao.search(Machine.getSearchSql(machine));
					  req.getSession().setAttribute("machines", machines);
					  
					 Supplier supplier= new Supplier(); supplier.setOwner(ownerExist.getTelnum());
					 SupplierDao supplierDao = new SupplierDao(conn);
					  List<Supplier> suppliers= supplierDao.search(Supplier.getSearchSql(supplier));				 
					  req.getSession().setAttribute("suppliers", suppliers);
				   
				   
				   
				   Common.redirectToJsp(req,resp,req.getContextPath()+"/owner/owner.jsp"); return;
			   }else{
				   Common.forwardToJsp(req,resp,jumpJspPath,"密码不正确"); return;
			   }
		   }
		  
		}catch(Exception e){
			e.printStackTrace();
			  Common.forwardToJsp(req,resp,jumpJspPath,"数据库异常"); return;
		}finally{
			 ConnFactory.closeConn(conn);	
		}

   
	}
	
/*------------------------------------------新建自提柜分配-------------------------------------------------*/	
	
	public static void createDistribute(HttpServletRequest req, HttpServletResponse resp){		
		
		
		
	    String	jumpJspPath= "/owner/createDistribute.jsp";
	    Owner owner;
	
		//获取Bean---------------------------	
		 try{
	    owner= Owner.beanFromRequest(req);
		 }catch(Exception e){
			 e.printStackTrace();
			  Common.forwardToJsp(req,resp,jumpJspPath,"输入参数错误");
			  return;
		 }
		//调用 dao处理事务 -------------------------
		Connection conn = null;
		try{			
		   conn= ConnFactory.getConn();	
		  MachineDao machineDao= new MachineDao(conn);
		  Machine machine= new Machine();  machine.setOwner(owner.getTelnum());
		  List<Machine> machines= machineDao.search(Machine.getSearchSql(machine));
		 Supplier supplier= new Supplier(); supplier.setOwner(owner.getTelnum());
		 SupplierDao supplierDao = new SupplierDao(conn);
		  List<Supplier> suppliers= supplierDao.search(Supplier.getSearchSql(supplier));
		  req.getSession().setAttribute("machines", machines);
		  req.getSession().setAttribute("suppliers", suppliers);
		  req.setAttribute("success", true);
		  Common.forwardToJsp(req,resp,jumpJspPath);
		}catch(Exception e){
			e.printStackTrace();
			  Common.forwardToJsp(req,resp,jumpJspPath,"数据库异常"); return;
		}finally{
			 ConnFactory.closeConn(conn);	
		}

   
	}
		

/*------------------------------------------保存自提柜分配-------------------------------------------------*/	
	
	public static void saveDistribute(HttpServletRequest req, HttpServletResponse resp){
		
		// 获取参数,构造 bean;
		  String	jumpJspPath= "/owner/createDistribute.jsp";
	//	List<Integer> machines= new ArrayList<Integer>();
	//	List<String> suppliers= new ArrayList<String>();
	//	List<Integer> times= new ArrayList<Integer>();
		List<Distribute>  distributes= new ArrayList<Distribute>();
 		try{
 		//	List<Machine> machinesSession= (List<Machine>) req.getSession().getAttribute("machines");
 		//	List<Supplier> suppliersSession= (List<Supplier>) req.getSession().getAttribute("suppliers");
		
 			String[] machines = req.getParameterValues("machine");   
 			String[] suppliers = req.getParameterValues("supplier");
 			String[] times = req.getParameterValues("time");
		
		
		for(int i=0;i<suppliers.length;i++){
			for(int j=0;j<machines.length;j++){
				for(int k=0;k<times.length;k++){
					Distribute distribute= new Distribute();
					distribute.setSupplier(suppliers[i]);
					int machineID= Integer.parseInt(machines[j]);
					distribute.setMachine(machineID);
					int timeFlag= Integer.parseInt(times[k]);
					distribute.setTimeFlag(timeFlag);
					
					List<Machine> machineBeans= ( List<Machine>)req.getSession().getAttribute("machines");
					for(int p=0;p<machineBeans.size();p++){
						if(machineBeans.get(p).getId()==machineID){
							if(timeFlag==1){distribute.setTimeAA(machineBeans.get(p).getTimeAAM());}
							if(timeFlag==2){distribute.setTimeAA(machineBeans.get(p).getTimeAAN());}
							if(timeFlag==3){distribute.setTimeAA(machineBeans.get(p).getTimeAAE());}
						}
					}
					
					distributes.add(distribute);
				}
			}
		}
 		}catch(Exception e){
 			 e.printStackTrace();
 			 req.setAttribute("success", true);
			 Common.forwardToJsp(req,resp,jumpJspPath,"失败，输入参数有误");
			 return;
 		}
	// 调用Dao处理
		Connection conn = null;
		
		try{
			 conn= ConnFactory.getConn();
			 DistributeDao distributeDao= new DistributeDao(conn);
			distributeDao.create(distributes);
			 req.setAttribute("success", true);
			 Common.forwardToJsp(req,resp,jumpJspPath,"创建成功");
			 
		}catch(Exception e){
			e.printStackTrace();
			 req.setAttribute("success", true);
			 Common.forwardToJsp(req,resp,jumpJspPath,"失败，数据库异常");
			 return;
		}finally{
			 ConnFactory.closeConn(conn);	
 		}
		
		
	}	
	
/*------------------------------------------搜索自提柜分配-------------------------------------------------*/	
	
	public static void searchDistribute(HttpServletRequest req, HttpServletResponse resp){
		
		Distribute distribute;
		Owner ownerLogined;
		String	jumpJspPath= "/owner/manageDistribute.jsp";
		List<Distribute>  distributes= new ArrayList<Distribute>();
		//获取bean 
		try{
			ownerLogined=(Owner) req.getSession().getAttribute("ownerLogined");
			distribute= Distribute.beanFromRequest(req);
			distribute.setOwner(ownerLogined.getTelnum());
		}catch(Exception e){
			e.printStackTrace();  Common.forwardToJsp(req,resp,jumpJspPath,"失败，输入参数有误"); return;
		}
		
		// 调用Dao处理
				Connection conn = null;
				
				try{
					 conn= ConnFactory.getConn();				
					  DistributeDao distributeDao= new DistributeDao(conn);
					  
					/* if(distribute.getMachine()==0&&distribute.getSupplier().isEmpty()){
						  for(int i=0;i<suppliers.size();i++){
								distribute.setSupplier(suppliers.get(i).getTelnum());
							List<Distribute> distributesTemp = distributeDao.search(Distribute.getSearchSql(distribute));
						    for(int j=0;j<distributesTemp.size();j++){
						    	distributes.add(distributesTemp.get(j));
						    }
						  } 
						 req.setAttribute("success", true);
						 req.getSession().setAttribute("distributes", distributes);  
					 }else{ }*/
						distributes= distributeDao.search(DistributeDao.getSearchSql(distribute));
						 req.setAttribute("success", true);
						 req.getSession().setAttribute("distributes", distributes); 
					 
					 
					 Common.forwardToJsp(req,resp,jumpJspPath,"搜索成功");					 
				}catch(Exception e){
					e.printStackTrace();
					 req.setAttribute("success", true);
					 Common.forwardToJsp(req,resp,jumpJspPath,"失败，数据库异常");
					 return;
				}finally{
					 ConnFactory.closeConn(conn);	
		 		}
	}	
	
}
